/**
 * 
 */
package org.gnf.genewiki.stream;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.gnf.genewiki.GeneWikiLink;
import org.gnf.genewiki.GeneWikiPage;
import org.gnf.genewiki.Heading;
import org.gnf.genewiki.Reference;
import org.gnf.genewiki.Sentence;
import org.gnf.genewiki.metrics.RevisionCounter;
import org.gnf.ncbo.web.AnnotatorClient;
import org.gnf.ncbo.web.NcboAnnotation;
import org.gnf.util.TextFun;

/**
 * @author bgood
 *
 */
public class Tweetables {
	List<Tweetable> tweets;
	String summaryForRSS;
	
	/**
	 * Given two genewikipage's (revisions for the same article) emit a compressed summary of the meaningful changes that they contain
	 * @param p_t0
	 * @param p_t1
	 */
	public Tweetables(GeneWikiPage p_t0, GeneWikiPage p_t1, RevisionCounter rc){
		//for change summarization attempt using NCBO annotator...
		boolean useGO = true; boolean useDO = true; boolean useFMA = true; boolean usePRO = true;

		tweets = new ArrayList<Tweetable>();
		Tweetable base = new Tweetable(p_t0, p_t1);
		if(base.user.toLowerCase().contains("bot")){
			return;
		}
		summaryForRSS = " The article on <a href=\"http://en.wikipedia.org/wiki/"+base.getHashtag().substring(1)+"\">"+base.getHashtag().substring(1)+"</a> ";
		//reference tweet
		if(base.references>0){
			String summary = "";
			summary += base.user+" added "+base.references+" refs to "+base.hashtag+", PMIDS: ";
			summaryForRSS += "grew by "+base.references+" refs. PMIDS: ";
			List<String> pmid0 = new ArrayList<String>();
			for(Reference ref : p_t0.getRefs()){
				if(ref.getPmid()!=null){
					pmid0.add(ref.getPmid());
				}
			}
			Set<String> newones = new HashSet<String>();
			for(Reference ref : p_t1.getRefs()){
				if(ref.getPmid()!=null){
					if(!pmid0.contains(ref.getPmid())&&newones.add(ref.getPmid())){
						if((summary.length()+ref.getPmid().length())>115){
							summary = summary.substring(0,summary.length()-1);
							summary+="..";
							break;
						}
						summary += ref.getPmid()+",";
						summaryForRSS += "<a href=\"http://www.ncbi.nlm.nih.gov/pubmed/"+ref.getPmid()+"\">"+ref.getPmid()+"</a>,"; 
					}
				}
			}
			if(summary.endsWith(","))summary = summary.substring(0,summary.length()-1);


			Tweetable tweet = new Tweetable(base);
			tweet.setSummary(summary);
			tweets.add(tweet);		
		}
		//BogHog tweet - removing PBB_Summary 
		if(p_t0.getPageContent().contains("PBB_Summary")&&!p_t1.getPageContent().contains("PBB_Summary")){
			Tweetable tweet = new Tweetable(base);
			tweet.setSummary(base.user+" removed the autogenerated text summary from "+base.hashtag);
			tweets.add(tweet);	
		}
		//likely vandal removing PBB template {{PBB|geneid=7434}}
		if(p_t0.getPageContent().contains("{{PBB|geneid="+p_t0.getNcbi_gene_id())&&!p_t1.getPageContent().contains("{{PBB|geneid="+p_t1.getNcbi_gene_id())){
			Tweetable tweet = new Tweetable(base);
			tweet.setSummary(base.user+" removed the autogenerated infobox from "+base.hashtag);
			tweets.add(tweet);	
		}
		//media tweet
		if(base.media>0){
			String summary = "";
			summary += base.user+" added "+base.media+" new media files to "+base.hashtag;
			summaryForRSS += " was enhanced with "+base.media+" new media files, ";

			Tweetable tweet = new Tweetable(base);
			tweet.setSummary(summary);
			tweets.add(tweet);	
		}
		//headings tweet
		if(base.headings>0){
			String summary = "";
			summary += base.user+" added "+base.headings+" new headings to "+base.hashtag+" - ";
			summaryForRSS += "gained a new section on ";
			List<String> head = new ArrayList<String>();
			for(Heading h : p_t0.getHeadings()){
				if(h.getPrettyText()!=null){
					head.add(h.getPrettyText());
				}
			}
			for(Heading h : p_t1.getHeadings()){
				if(h.getPrettyText()!=null){
					if(!head.contains(h.getPrettyText())){
						summary+=h.getPrettyText()+" ";
						summaryForRSS+="'"+h.getPrettyText()+"'";
					}
				}
			}
			summaryForRSS+=", ";
			Tweetable tweet = new Tweetable(base);
			tweet.setSummary(summary);
			tweets.add(tweet);	
		}
		//out links tweet
		if(base.out_links>0){
			Set<String> oldlinks = new HashSet<String>();
			for(GeneWikiLink glink : p_t0.getGlinks()){
				if(glink.getTarget_page()!=null&&glink.getTarget_page().length()>4){
					oldlinks.add(glink.getTarget_page());
				}
			}
			Set<String> newlinks = new HashSet<String>();
			for(GeneWikiLink glink : p_t1.getGlinks()){
				if(glink.getTarget_page()!=null&&glink.getTarget_page().length()>4){
					newlinks.add(glink.getTarget_page());
				}
			}
			newlinks.removeAll(oldlinks);
			if(newlinks!=null&&newlinks.size()>0){
				String summary = "";
				summary+= base.user+" linked "+base.hashtag+" to ";
				summaryForRSS += " was linked to ";
				for(String link : newlinks){
					//make sure there is room for the link
					if((summary.length()+link.length())>115){
						summary+="...";
						break;
					}
					summary+="#"+TextFun.makeHashTagSafe(link)+" ";
					//	summaryForRSS+=link+", "; http://bit.ly/hC19Rl
					summaryForRSS += "<a href=\"http://en.wikipedia.org/wiki/"+link+"\">"+link+"</a>, "; 
				}
				if(summary.endsWith(","))summary = summary.substring(0,summary.length()-1);

				Tweetable tweet = new Tweetable(base);
				tweet.setSummary(summary);
				tweets.add(tweet);	
			}
			//	else{
			//likely added a link somewhere else in the text that was already present
			//ex) http://en.wikipedia.org/wiki/?action=historysubmit&diff=414018948&oldid=414018280
			//		System.err.println("link processing error found for "+base.getDifflink());
			//	}
		}
		if(base.words>0&&base.sentences>0){
			String summary = "";
			summary+= base.user+" added "+base.words+" words to "+base.hashtag+" ";
			Set<String> keywords = new HashSet<String>();
			List<String> diffs = rc.getDiffBlocks(p_t1.getRevid(), p_t0.getRevid());
			if(diffs!=null&&diffs.size()>0){
				for(String diff : diffs){
					List<NcboAnnotation> annos = AnnotatorClient.ncboAnnotateText(diff, true, useGO, useDO, useFMA, usePRO);
					for(NcboAnnotation anno : annos){
						keywords.add(anno.getConcept().getPreferredName());
					}
				}
			}

			if(keywords.size()>0){
				summary += "- ";
				for(String keyword : keywords){
					if((summary.length()+keyword.length())>115){
						summary+="...";
						break;
					}else{
						summary += "#"+TextFun.makeHashTagSafe(keyword)+" ";
					}
				}
			}


			Tweetable tweet = new Tweetable(base);
			tweet.setSummary(summary);
			tweets.add(tweet);	

			summaryForRSS += " and added "+base.words+" words.";
		}
		//add comment tweet
		if(base.getUser_comment()!=null){
			String comment = base.getUser_comment().trim();
			if(comment.length()>3){
				String summary = "";
				summary+= base.user+" edited "+base.hashtag+" : "+base.getUser_comment();
				if(summary.length()>119){
					summary = summary.substring(0,116)+"...";
				}				
				Tweetable tweet = new Tweetable(base);
				tweet.setSummary(summary);
				tweets.add(tweet);	
				summaryForRSS += " and added "+base.bytes+" bytes.";
			}
		}
		//default to bytes tweet
		if(tweets.size()==0&&base.bytes!=0){
			String summary = "";
			summary+= base.user+" edited "+base.hashtag+" ";
			if(base.bytes>0){
				summary += "adding "+base.bytes+" bytes.";
			}else{
				summary += "deleting "+(-1*base.bytes)+" bytes.";
			}
			Tweetable tweet = new Tweetable(base);
			tweet.setSummary(summary);
			tweets.add(tweet);	
			summaryForRSS += " and added "+base.bytes+" bytes.";
		}
	


}

public List<Tweetable> getTweets() {
	return tweets;
}

public void setTweets(List<Tweetable> tweets) {
	this.tweets = tweets;
}

public String getSummaryForRSS() {
	return summaryForRSS;
}

public void setSummaryForRSS(String summaryForRSS) {
	this.summaryForRSS = summaryForRSS;
}
}
